<template>
  <div class="app-container">
    <el-row type="flex" align="middle">
      <el-col>
        <div style="display: flex; align-items: center;" class="dropdown">
          <span class="frequency">刷新频率:</span>
          <el-dropdown size="small" split-button type="primary" class="dropdown" @command="handleCommand">
            {{ selectedTime }}
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="time in timeOptions" :key="time" :command="time">{{ time }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="工单名称" prop="workOrderName">
        <el-input
          v-model="queryParams.workOrderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序顺序" prop="productionLineSequence">
        <el-input
          v-model="queryParams.productionLineSequence"
          placeholder="请输入工序顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="预排时间" prop="beginTime">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.beginTime"-->
<!--          type="date"-->
<!--          placeholder="选择日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="动作状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择动作状态" clearable>
          <el-option v-for="dict in acctionStatus" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zndd:walkthroughs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="walkthroughsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="70"/>
<!--      <el-table-column label="动作id" align="center" prop="actionId" width="80"/>-->
      <el-table-column label="任务名称" align="center" prop="taskName"  width="180"/>
      <el-table-column label="工单名称" align="center" prop="workOrderName" width="180" show-overflow-tooltip/>
      <el-table-column label="工单开始时间" align="center" prop="orderStartTime" width="150"/>
      <el-table-column label="工单结束时间" align="center" prop="orderEndTime" width="150"/>
      <el-table-column label="工序名称" align="center" prop="processName" width="150"/>
      <el-table-column label="工序顺序" align="center" prop="productionLineSequence" width="130"/>
      <el-table-column label="设备名称" align="center" prop="deviceName" width="150"/>
      <el-table-column label="设备类型" align="center" prop="deviceType" width="130"/>
      <el-table-column label="动作状态" align="center" prop="status" :formatter="acctionStatusFomatter" width="130"/>
      <el-table-column label="焊缝区域X" align="center" prop="weldZoneX" width="130"/>
      <el-table-column label="焊缝区域Y" align="center" prop="weldZoneY" width="130"/>
      <el-table-column label="焊缝长度(mm)" align="center" prop="weldLength" width="130"/>
      <el-table-column label="设备的动作顺序" align="center" prop="deviceActionOrder" width="130"/>
      <el-table-column label="预计开始时间" align="center" prop="exStartTime" width="150"/>
      <el-table-column label="预计结束时间" align="center" prop="exEndTime" width="150"/>
      <el-table-column label="实际开始时间" align="center" prop="acStartTime" width="150"/>
      <el-table-column label="实际结束时间" align="center" prop="acEndTime" width="150"/>
      <el-table-column label="备注" align="center" prop="remark" width="150" show-overflow-tooltip/>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zndd:walkthroughs:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zndd:walkthroughs:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改动作调度预排对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务id" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入任务id" />
        </el-form-item>
        <el-form-item label="工序id" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工序id" />
        </el-form-item>
        <el-form-item label="工单id" prop="workOrderId">
          <el-input v-model="form.workOrderId" placeholder="请输入工单id" />
        </el-form-item>
        <el-form-item label="动作id" prop="actionId">
          <el-input v-model="form.actionId" placeholder="请输入动作id" />
        </el-form-item>
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="每个设备的动作顺序" prop="deviceActionOrder">
          <el-input v-model="form.deviceActionOrder" placeholder="请输入每个设备的动作顺序" />
        </el-form-item>
        <el-form-item label="预排时间" prop="frequencyTime">
          <el-input v-model="form.frequencyTime" placeholder="请输入预排时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWalkthroughs, getWalkthroughs, delWalkthroughs, addWalkthroughs, updateWalkthroughs } from "@/api/zndd/action/walkthroughs";

export default {
  name: "Walkthroughs",
  data() {
    return {
      selectedTime: '1秒钟',
      allTimes: ['1秒钟','5秒钟', '10秒钟', '20秒钟', '30秒钟', '50秒钟'],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //动作状态
      acctionStatus: [],
      // 动作调度预排表格数据
      walkthroughsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        processId: null,
        workOrderId: null,
        actionId: null,
        deviceId: null,
        deviceActionOrder: null,
        frequencyTime: null,
        status: null,
        taskName: '',
        workOrderName: '',
        productionLineSequence: null,
        beginTime: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskId: [
          { required: true, message: "任务id不能为空", trigger: "blur" }
        ],
        processId: [
          { required: true, message: "工序id不能为空", trigger: "blur" }
        ],
        workOrderId: [
          { required: true, message: "工单id不能为空", trigger: "blur" }
        ],
        actionId: [
          { required: true, message: "动作id不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备id不能为空", trigger: "blur" }
        ],
        deviceActionOrder: [
          { required: true, message: "每个设备的动作顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  computed: {
    timeOptions() {
      return this.allTimes.filter(time => time !== this.selectedTime);
    },
    timeInMilliseconds() {
      const seconds = parseInt(this.selectedTime.replace('秒钟', ''), 10);
      return seconds * 1000;
    },
  },
  created() {
    //获取字典表中工单状态
    this.getDicts("zndd_walkthroughs_status").then(response => {
      this.acctionStatus = response.data;
    });
    this.getList();
    // 初始化定时器
    this.startTimer();
  },
  beforeDestroy() {
    // 清除定时器
    this.clearTimer();
  },
  watch: {
    selectedTime() {
      // 当 selectedTime 改变时，重新设置定时器
      this.resetTimer();
    },
    activeName(newVal) {
      this.expandedItems = {};
      newVal.forEach(name => {
        this.expandedItems[name] = true;
      });
    }
  },
  methods: {
    //格式化工单状态
    acctionStatusFomatter(row, column){
      var ws = null;
      console.log(this.workStauts)
      this.acctionStatus.forEach(status => {
        if (status.dictValue == row.status) {
          ws = status;
        }
      });
      if(ws == null){
        return "";
      }else{
        if(ws.dictValue==0){
          return <el-tag  type="danger">未开始</el-tag>;
        }else if(ws.dictValue==1){
          return <el-tag type="success">进行中</el-tag>;
        }else if(ws.dictValue==2){
          return <el-tag type="info">已完成</el-tag>;
        }
        return ws.dictLabel;
      }
    },



    // 添加一个日期格式化方法
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    /** 查询动作调度预排列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams.beginTime)
      listWalkthroughs(this.queryParams).then(response => {
        this.walkthroughsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCommand(command) {
      this.selectedTime = command;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        processId: null,
        workOrderId: null,
        actionId: null,
        deviceId: null,
        deviceActionOrder: null,
        frequencyTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.beginTime) {
        this.queryParams.beginTime = this.formatDate(this.queryParams.beginTime); // 格式化日期
      } else {
        this.queryParams.beginTime = ''; // 如果没有选择日期，则清空值
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加动作调度预排";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWalkthroughs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动作调度预排";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWalkthroughs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWalkthroughs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //定时器相关
    startTimer() {
      this.timer = setInterval(() => {
        this.getList();
      }, this.timeInMilliseconds);
    },
    clearTimer() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    resetTimer() {
      this.clearTimer();
      this.startTimer();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除动作调度预排编号为"' + ids + '"的数据项？').then(function() {
        return delWalkthroughs(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/walkthroughs/export', {
        ...this.queryParams
      }, `walkthroughs_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="css" scoped>
.dropdown{
  float: right;
  margin-bottom: 10px;
}
/* 自定义滚动条样式 */
::v-deep ::-webkit-scrollbar {
  width: 12px; /* 水平滚动条的宽度 */
  height: 12px; /* 垂直滚动条的高度 */
  cursor: pointer; /* 鼠标靠近滚动条时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-track {
  background: #f1f1f1; /* 滚动条轨道的颜色 */
  cursor: pointer; /* 鼠标靠近滚动条轨道时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb {
  background: #888; /* 滚动条滑块的颜色 */
  border-radius: 6px; /* 滚动条滑块的圆角 */
  cursor: pointer; /* 鼠标靠近滚动条滑块时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb:hover {
  background: #555; /* 滚动条滑块悬停时的颜色 */
  cursor: pointer; /* 鼠标悬停在滚动条滑块上时仍保持小手样式 */
}
.node-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
